package com.ts.utils

import android.os.HandlerThread
import android.util.Size
import androidx.camera.core.CameraX
import androidx.camera.core.CameraX.LensFacing
import androidx.camera.core.Preview
import androidx.camera.core.Preview.OnPreviewOutputUpdateListener
import androidx.camera.core.PreviewConfig
import androidx.lifecycle.LifecycleOwner

class CameraXHelper {
    var handlerThread: HandlerThread?=null
    private val currentFacing = LensFacing.BACK
    private var listener: OnPreviewOutputUpdateListener? = null


    constructor(lifecycleOwner: LifecycleOwner, listener: Preview.OnPreviewOutputUpdateListener ){
        this.listener = listener
        handlerThread = HandlerThread("Analyze-thread")
        handlerThread?.start()
        CameraX.bindToLifecycle(lifecycleOwner,getPreView())
    }

    private fun getPreView(): Preview {
        // 分辨率并不是最终的分辨率，CameraX会自动根据设备的支持情况，结合你的参数，设置一个最为接近的分辨率
        val previewConfig = PreviewConfig.Builder()
            .setTargetResolution(Size(640, 480))
            .setLensFacing(currentFacing) //前置或者后置摄像头
            .build()

        //        要不  得到他的数据
        val preview = Preview(previewConfig)
        preview.onPreviewOutputUpdateListener = listener
        return preview
    }
}